package shopping.manager;

import google.docs.log_in;
import list.ListEdit;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import date.base.DbHelper;

public class ShoppingManager extends ListActivity {
    /** Called when the activity is first created. */
	private DbHelper DbHelper;
	private static final int ACTIVITY_EDIT=1;
	private long ListId;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DbHelper = new DbHelper(this);
        DbHelper.open();
        fillData();
        registerForContextMenu(getListView());
        //DbHelper.close();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.home_menu, menu);
    	return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	if(item.getItemId() == R.id.addList){
    		//Toast.makeText(main.this, "Add Button",Toast.LENGTH_SHORT).show();
    		addList();
    	}
    	else if(item.getItemId() == R.id.deleteList){
    		Toast.makeText(ShoppingManager.this, "Delete Button",Toast.LENGTH_SHORT).show();
    		DbHelper.removeAllLists();
			fillData();
    	}
    	else if(item.getItemId() == R.id.importList){
    		importList();
    		fillData();
    	}
    	return super.onOptionsItemSelected(item);
    }
    
    private void importList() {
    	Intent i = new Intent(this, log_in.class);
		startActivity(i);
		
	}

	private void addList() {
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setCancelable(false);
		final EditText input =(EditText) new EditText(this);
		input.setHint("Enter name of the new list..");
		input.setSingleLine(true);
		
		dialog.setView(input);
		dialog.setPositiveButton("Save", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String listName = input.getText().toString();
				if (listName.length()==0) {
					dialog.dismiss();
					Toast.makeText(ShoppingManager.this, "You must provide an input", Toast.LENGTH_SHORT).show();
				} else{
					//Toast.makeText(ShoppingManager.this, "You are willing to create.", Toast.LENGTH_LONG).show();
					DbHelper.createList(listName);
					fillData();
					dialog.dismiss();
				}
			}

		});
		
		
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		
		dialog.show();
		
	}
    
    
    //To get lists from the list table in database
    private void fillData() {
 		Cursor listsCursor = DbHelper.getAllLists();
 		startManagingCursor(listsCursor);
 		
 		/*
 		 * This create two arrays, from and to. It maps where column form data base should be displayed
 		 */
 		String[] from = new String[] {DbHelper.LIST_NAME};
 		int[] to = new int[] {R.id.ListRowText};
 		
 		SimpleCursorAdapter shoppingLists = new SimpleCursorAdapter(this, R.layout.list_view, listsCursor, from, to);
 		setListAdapter(shoppingLists);
 		
 	}
     
    
     //activity for editing
     @Override
 	protected void onListItemClick(ListView l, View v, int position, long id) {
 		super.onListItemClick(l, v, position, id);
 		Intent i = new Intent(this, ListEdit.class);
 		i.putExtra(DbHelper.LIST_ROWID, id);
 		startActivityForResult(i, ACTIVITY_EDIT);
 	}
     
     @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
    		ContextMenuInfo menuInfo) {
    	MenuInflater inflater = getMenuInflater();
 		inflater.inflate(R.menu.list_context_menu,menu);
    	super.onCreateContextMenu(menu, v, menuInfo);
    }
    
     @Override
    public boolean onContextItemSelected(MenuItem item) {
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    	ListId = info.id;
    	if(item.getItemId() == R.id.renameList){
    		renameDialog();
    	}
    	else if(item.getItemId() == R.id.deleteList){
    		deleteConfirm();
    	}
    	else if(item.getItemId() == R.id.exportList){
    		
    	}
    	return super.onContextItemSelected(item);
    }
     
     private void deleteConfirm() {
 		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
 		dialog.setMessage("Are you sure?");
 		dialog.setCancelable(false);
 		dialog.setPositiveButton("Yes", new OnClickListener() {
 			
 			@Override
 			public void onClick(DialogInterface dialog, int which) {
 				Cursor c = (Cursor) getListView().getAdapter().getItem(which);
 				
 				//Toast.makeText(ListEdit.this, "Table name: " + TableName, Toast.LENGTH_SHORT).show();
 				//Toast.makeText(ListEdit.this, "Item id: " + ItemId, Toast.LENGTH_SHORT).show();
 				DbHelper.removeList(ListId);
 				c.requery();
 				fillData();
 			}
 		});
 		dialog.setNegativeButton("No", new OnClickListener() {
 			
 			@Override
 			public void onClick(DialogInterface dialog, int which) {
 				// TODO Auto-generated method stub
 				dialog.cancel();
 			}
 		});
 		AlertDialog alertDialog = dialog.create();
 		alertDialog.show();
 		
 	}
     
     public void renameDialog(){
    	 Cursor c = DbHelper.getList(ListId);
    	 String name = c.getString(DbHelper.LIST_NAME_COLUMN);
    	 AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    	 dialog.setCancelable(false);
    	 final EditText input =(EditText) new EditText(this);
    	 input.setText(name);
    	 input.setSelectAllOnFocus(true);
    	 input.setSingleLine(true);
    	 dialog.setView(input);
    	 dialog.setPositiveButton("Save", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String listName = input.getText().toString();
				if (listName.length() == 0) {
					dialog.dismiss();
					Toast.makeText(ShoppingManager.this, "You must provide and input", Toast.LENGTH_LONG).show();
				} else {
					DbHelper.setListName(ListId, listName);
					fillData();
					dialog.dismiss();
					fillData();
				}
			}
		});
    	 
    	 dialog.setNegativeButton("Cancel", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
    	 dialog.show();
  		
     }
      
}